package com.example.analysis.service.impl;

import com.example.analysis.entity.Student;
import com.example.analysis.entity.Supervision;
import com.example.analysis.entity.Teacher;
import com.example.analysis.mapper.StudentDao;
import com.example.analysis.mapper.SupervisionDao;
import com.example.analysis.mapper.TeacherDao;
import com.example.analysis.service.UserService;
import com.example.analysis.vo.UserVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private SupervisionDao supervisionDao;

    @Override
    public boolean login(UserVO user) {
        int role = user.getRole();
        System.out.println(role);
        if(role == 3) {
            Supervision supervision = supervisionDao.selectByPrimaryKey(user.getUsername());
            return supervision != null && supervision.getPwd().equals(user.getPassword());
        } else if (role == 2){
            Teacher teacher = teacherDao.selectByPrimaryKey(user.getUsername());
            return teacher != null && teacher.getPwd().equals(user.getPassword());
        } else if (role == 1) {
            Student student = studentDao.selectByPrimaryKey(user.getUsername());
            return student != null && student.getPwd().equals(user.getPassword());
        }
        return false;
    }

    @Override
    public UserVO getInfo(String token) {
        String[] strings = token.split("\\+");
        int role = Integer.parseInt(strings[1]);
        String userId = strings[0];
        UserVO userVO = new UserVO();
        userVO.setRole(role);
        switch (role) {
            case 3 -> {
                Supervision supervision = supervisionDao.selectByPrimaryKey(userId);
                userVO.setName(supervision.getSupervisionName());
                userVO.setUsername(supervision.getSupervisionId());
            }
            case 2 -> {
                Teacher teacher = teacherDao.selectByPrimaryKey(userId);
                userVO.setName(teacher.getTeacherName());
                userVO.setUsername(teacher.getTeacherId());
            }
            case 1 -> {
                Student student = studentDao.selectByPrimaryKey(userId);
                userVO.setName(student.getName());
                userVO.setUsername(student.getStudentId());
            }
            default -> {
                return null;
            }
        }
        userVO.setAvatar("https://img1.baidu.com/it/u=1997177228,4202492155&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        return userVO;
    }
}
